


#include <string>
#include <QImage>
#include <QDebug>
#include "VideoDecoder.h" // 引入AVFormatContextWrapper类，如果需要
extern "C" {
#include <libswscale/swscale.h>
}
#include "Media/Sync/sync.h"
VideoDecoder::VideoDecoder() {
//     qDebug()<<__FUNCTION__;
}

VideoDecoder::~VideoDecoder() {
    release();
}
bool VideoDecoder::init(const AVCodecContextPtr& codec_ctx) {
    codec_ctx_ = codec_ctx.get();
    //isRunning_ = true;
    return true;
}
void VideoDecoder::adjustPlaybackSpeed(int speedPercentage) {
    // 检查速度百分比是否在合理范围内
    if (speedPercentage < 0 || speedPercentage > 200) {
        std::cout << "Error: speedPercentage should be between 0 and 200" << std::endl;
        return;
    }
     std::cout  <<__FUNCTION__<< "old framerate" << codec_ctx_->framerate.num <<std::endl;
     // 如果framerate为0，设置一个默认的帧率
     if (codec_ctx_->framerate.num == 0) {
         codec_ctx_->framerate.num = 24;
         codec_ctx_->framerate.den = 1;
         return ;
     }
    // 计算新的帧率
    AVRational newFrameRate;
    newFrameRate.num = codec_ctx_->framerate.num * speedPercentage;
    newFrameRate.den = codec_ctx_->framerate.den * 100;

    // 检查新的帧率是否在合理范围内
    if (newFrameRate.num < 1 || newFrameRate.num > codec_ctx_->framerate.num * 2) {
        std::cout << "Error: newFrameRate should be between 1 and " << codec_ctx_->framerate.num * 2 << std::endl;
        return;
    }

    // 设置新的帧率
    codec_ctx_->framerate = newFrameRate;
       std::cout <<__FUNCTION__<< "new framerate" << codec_ctx_->framerate.num <<std::endl;
}


struct AVFrameDeleter {
    void operator()(AVFrame* frame) const {
        av_frame_free(&frame);
    }
};


bool VideoDecoder::decode(AVPacket* packet) {
    if (!codec_ctx_ || !packet) {
        std::cout << "Error: codec_ctx_ or packet is null" << std::endl;
        return false;
    }
    int ret = avcodec_send_packet(const_cast<AVCodecContext*>(codec_ctx_), packet);
    if (ret < 0) {
        std::cout << "Error: avcodec_send_packet failed, ret = " << ret << std::endl;
        return false;
    }
    std::unique_ptr<AVFrame, AVFrameDeleter> frame(av_frame_alloc());
//    if (!frame) {
//        std::cout << "Error: av_frame_alloc failed" << std::endl;
//        return false;
//    }
    while (true) {
        ret = avcodec_receive_frame(const_cast<AVCodecContext*>(codec_ctx_), frame.get());
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            break;
        } else if (ret < 0) {
            std::cout << "Error: avcodec_receive_frame failed, ret = " << ret << std::endl;
            return false;
        }
        AVSync::getInstance().add_video_frame(frame.get());
        av_frame_unref(frame.get());
    }

    return true;
}

void VideoDecoder::release() {
    //isRunning_ = false;
    // if (decodeThread_.joinable()) {
    //     decodeThread_.join();
    // }
    if (codec_ctx_) {
        codec_ctx_ = nullptr;
    }
}
